<?php
class ContractManageController extends CAdmin {
	function __construct(){
		parent::__construct();
	}
	
	public function init(){
		$cache = new Caches();
		$option_cache = $cache->getcache('option', 'commons');
		include self::admin_tpl('contract_list');
	}
	
	public function ajx_init(){
		$cond = '';
		//搜索条件处理
		$pro = new CProcess();
		$cond = $pro->getSearchFromPost($_POST);
		
		$contractdb = new Contract_Model();
		$page = isset($_GET['page']) ? intval($_GET['page']) : '1';
		$userid = $_SESSION['userid'];
		$pagesize = 2;	
		$setype = ROUTE_C;
		$where ="contract_id in (select crmid from r_crmentity where (smcreatorid = '$userid' or smsharerid ='$userid') and setype = '$setype' and deleted = 0)".$cond;
		$datas = $contractdb->listinfo($where,"customer_id,contract_id,contract_name,contract_pay_mode,contract_num,contract_sum,contract_type", '', $page, $pagesize);
		$cache = new Caches();
		$option_cache = $cache->getcache('option', 'commons');
		$page_data = array();
		foreach ($datas as $data) {			
			$data['contract_pay_mode'] = $option_cache['contract_pay_mode'][$data['contract_pay_mode']];
			$data['contract_type'] = $option_cache['contract_type'][$data['contract_type']];
			$page_data[] = $data;
		}
		$head = "contract_num,contract_name,contract_pay_mode,contract_sum,contract_type";
		$page_head = explode(',',$head);
		$page_state = $contractdb->pages; 		
		$page = array('page_data'=>$page_data,'page_head'=>$page_head,'page_state'=>$page_state);
		echo $infot = json_encode($page);
	}
	
	public function add(){
		if(isset($_POST['dosubmit'])){
			$contractdb = new Contract_Model();
			$crmentitydb = new CrmEntity_Model();
			$usergroupdb = new User_group_Model();
			$info = $this->checkinfo($_POST['info']);			
			$new_id = $contractdb->insert($info,true);
			$userid = $_SESSION['userid'];
			$setype = ROUTE_C;
			$parentid=$usergroupdb->get_one(array('userid'=>$userid),'parentid');
			if($parentid){
				$crmentitydb->insert(array('crmid'=>$new_id,'smcreatorid'=>$userid,'smsharerid'=>$parentid['parentid'],'setype'=>$setype,'createdtime'=>SYS_TIME));	
			} else {
				$crmentitydb->insert(array('crmid'=>$new_id,'smcreatorid'=>$userid,'setype'=>$setype,'createdtime'=>SYS_TIME));
			}						
			self::showmessage('操作成功！','?m=sale&c=contractmanage');
		}else {
			$cache = new Caches();
			$option_cache = $cache->getcache('option', 'commons');
			include self::admin_tpl('contract_add');
			
		}
	}
	
	public function edit(){
		if(isset($_POST['dosubmit'])){
			$contractdb = new Contract_Model();
			$crmentitydb = new CrmEntity_Model();
			$userid = $_SESSION['userid'];
			$info = $this->checkinfo($_POST['info']);
			$contract_id = intval($info['contract_id']);
//			$fields = array('contract_name','customer_id','contract_type','contract_num','contract_pay_mode','contract_sum');
//			foreach ($info as $k => $v) {
//				if(!in_array($k, $fields)){
//					unset($info[$k]);
//				}
//			}
			$contractdb->update($info,array('contract_id'=>$contract_id));
			$crmentitydb->update(array('modifiedby'=>$userid,'modifiedtime'=>SYS_TIME),array('crmid'=>$contract_id,'setype'=>ROUTE_C));
			self::showmessage('操作成功！','?m=sale&c=contractmanage');
		} else {
			$cache = new Caches();
			$option_cache = $cache->getcache('option', 'commons');
			$contractdb = new Contract_Model();
			$customerdb = new Customer_Model();
			$info = $contractdb->get_one(array('contract_id'=>$_GET['contract_id']));
			$customer_name = $customerdb->get_one(array('customer_id'=>$info['customer_id']),'customer_name');
			extract($info);
			include self::admin_tpl('contract_edit');
		}
	}
	
	/**
	 * 删除数据库和附件
	 * Enter description here ...
	 */
	public function delete(){
		$contract_id = intval($_POST['contract_id']);		
		$userid = $_SESSION['userid'];
		$crmentitydb = new CrmEntity_Model();
		$crmentitydb->update(array('deletedby'=>$userid,'deletedtime'=>SYS_TIME,'deleted'=>1),array('crmid'=>$contract_id,'setype'=>ROUTE_C));
		//等合同真正删除才能删附件
		$contractdb = new Contract_Model();
		$contract_num = $contractdb->get_one(array('contract_id'=>$contract_id),'contract_num');
		$Cattachment = new CAttachment(); 
		$Cattachment->delete_all($contract_num['contract_num']);
	}
	
	/**
	 * 上传
	 */
	public function upload(){		
		$Cattachment = new CAttachment();
		$contract_num = $_GET['_contract_num'];
		$aid = $Cattachment->upload('file', 0, $contract_num);
		if(!$aid){
			echo $Cattachment->error();
		} else {
			$att_index_db = new Attachment_index_Model();
			$att_index['contract_num'] = $contract_num;
			$att_index['aid'] = $aid;
			$att_index_db->insert($att_index);
			echo '1';
		} 
	}
	
	public function download(){
		$attach = new CAttachment();
		$attach->download();		
	}
	
	/**
	 * 附件删除
	 * Enter description here ...
	 */
	public function file_delete(){
		$aid = $_GET['att_id'];
		$attach = new CAttachment();
		if($attach->delete(array("aid"=>$aid))){
			echo '1';
		} else {
			echo '0';
		}		
	}
	
	/**
	 * 附件列表
	 */
	public function att_list_ajx(){
		$att_db =new Attachment_Model();
		$attach = new CAttachment();
		$contract_num = $_GET['_contract_num'];
		$where = "aid in (select aid from r_attachment_index where contract_num = '$contract_num')";
		$att_list = $att_db->select($where,'aid,filename,filepath,filesize');
		$attach_list = array();
		foreach ($att_list as $att) {
			$att['a_k']= $attach->get_downurl($att['filepath']);
			$attach_list[] = $att;
		}
		echo json_encode($attach_list);
	}
	
	
	private function checkinfo($info){
		$validator = new GlobalValidator();
		if(!is_array($info)){
			self::showmessage('参数不完整'); return false;
		}elseif(empty($info['contract_name']) || empty($info['contract_num'])){
			self::showmessage('2名不能空');
		}
		return $info;
	}
}